package com.suse.warmstill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.entity.TbResource;
import com.suse.warmstill.entity.TbStudent;
import com.suse.warmstill.mapper.TbStudentMapper;
import com.suse.warmstill.service.BaseService;
import com.suse.warmstill.service.StudentService;
import com.suse.warmstill.vo.ConditionVO;
import com.suse.warmstill.vo.ResourceVo;
import com.suse.warmstill.vo.StudentVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 黄靖
 * @author 2022-11-03 13:43
 * description:
 **/
@Service
public class StudentServiceImpl implements StudentService, BaseService<StudentVo, TbStudent> {

    @Autowired
    private TbStudentMapper studentMapper;


    @Override
    public CallResult<ListPageModel<TbStudent>> lisAll(ConditionVO conditionVO) {

        final LambdaQueryWrapper<TbStudent> wrapper = new LambdaQueryWrapper<>();
        final Page<TbStudent> page = new Page<>(conditionVO.getCurrent(), conditionVO.getSize());


        if (ObjectUtils.isNotEmpty(conditionVO.getKeyword())) {
            wrapper.like(TbStudent::getName, conditionVO.getKeyword());
        }
        final Page<TbStudent> tbResourcePage = studentMapper.selectPage(page, wrapper);

        final ListPageModel<TbStudent> resourceVoListPageModel = new ListPageModel<>();
        resourceVoListPageModel.setList(tbResourcePage.getRecords());
        resourceVoListPageModel.setCurrentPage((int) tbResourcePage.getCurrent());
        resourceVoListPageModel.setPageCount((int) tbResourcePage.getPages());
        resourceVoListPageModel.setPageSize((int) tbResourcePage.getSize());
        resourceVoListPageModel.setDataSize((int) tbResourcePage.getTotal());

        return CallResult.success(resourceVoListPageModel);
    }

    @Override
    public void insert(StudentVo studentVo) {
        final TbStudent tbStudent = toEntity(studentVo);
        final int i = studentMapper.insert(tbStudent);

    }


    @Override
    public TbStudent toEntity(StudentVo vo) {
        final TbStudent tbStudent = new TbStudent();
        tbStudent.setClasses(vo.getClasses());
        tbStudent.setCollege(vo.getCollege());
        tbStudent.setEmoStatus(vo.getEmoStatus());
        tbStudent.setGender(vo.getGender());
        tbStudent.setName(vo.getName());
        tbStudent.setNumber(vo.getNumber());
        tbStudent.setIsdel(0);
        tbStudent.setPhone(vo.getPhone());
        tbStudent.setProfession(vo.getProfession());
        return tbStudent;
    }
}
